package com.loto.insurance.oem.web.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.loto.insurance.oem.web.authority.OEMAuthority;
import com.loto.insurance.oem.web.entity.CustInfo;
import com.loto.insurance.oem.web.entity.Pager;
import com.loto.insurance.oem.web.entity.config.OEMConfig;
import com.loto.insurance.oem.web.enumeration.AuthorityRoleEnum;
import com.loto.insurance.oem.web.enumeration.ConfigFileEnum;
import com.loto.insurance.oem.web.enumeration.InsTypeEnum;
import com.loto.insurance.oem.web.enumeration.MenuEnum;
import com.loto.insurance.oem.web.enumeration.CustStatusEnum;
import com.loto.insurance.oem.web.enumeration.CustFromEnum;
import com.loto.insurance.oem.web.service.CustInfoService;
import com.loto.insurance.oem.web.utils.Config;
import com.loto.utils.DateUtils;
import com.loto.utils.StringUtils;

@Controller
@SessionAttributes("oem_user")
public class OemController {

  @Autowired
  CustInfoService custInfoService;

  @OEMAuthority(authorityRole = AuthorityRoleEnum.ADMIN)
  @RequestMapping(value = "/oem/info")
  public String oemInfo(HttpServletRequest request, Model model) {
	OEMConfig config = Config.getConfigFile(request, ConfigFileEnum.OEMCONFIG,
	    OEMConfig.class);
	if (config != null) {
	  model.addAttribute("config", config);
	}
	model.addAttribute("pageId", MenuEnum.OEM_INFO.getIndex());
	return "oem/info";
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.ADMIN)
  @RequestMapping(value = "/oem/userlist", method = RequestMethod.GET)
  public String userList(
	  @RequestParam(value = "custName", required = false) String custName,
	  @RequestParam(value = "custBegin", required = false) String custBegin,
	  @RequestParam(value = "custEnd", required = false) String custEnd,
	  @RequestParam(value = "custStatus", required = false, defaultValue = "-1") int custStatus,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  Model model) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (custName != null && !custName.isEmpty())
	  params.put("custName", custName);
	if (custBegin != null && custEnd != null
	    && DateUtils.isDate(custBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(custEnd, "yyyy-MM-dd")) {
	  params.put("custBegin", custBegin);
	  params.put("custEnd", custEnd);
	}
	if (custStatus > -1)
	  params.put("custStatus", custStatus);

	Pager pager = new Pager();
	if (params.size() > 0) {
	  pager.setParams(StringUtils.paramJoin(params));
	}

	model.addAllAttributes(params);

	if (params.size() == 0) {
	  Calendar now = Calendar.getInstance();
	  String today = DateUtils.dateToNormalString(now.getTime());
	  params.put("custBegin", today);
	  params.put("custEnd", today);
	}

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<CustInfo> custList = custInfoService.findAllCust(params);

	pager.setRowCount(custInfoService.findAllCustCount(params));

	model.addAttribute("custStatusList", CustStatusEnum.values());
	model.addAttribute("insTypeList", InsTypeEnum.values());
	model.addAttribute("pager", pager);
	model.addAttribute("custList", custList);

	model.addAttribute("pageId", MenuEnum.OEM_ACCOUNT_LIST.getIndex());
	return "oem/user_list";
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.ADMIN)
  @RequestMapping(value = "/oem/userdetail", method = RequestMethod.GET)
  public String oemUserDetail(@RequestParam(value = "id") long custId,
	  Model model) {
	model.addAttribute("cust", custInfoService.findById(custId));
	model.addAttribute("custExt", custInfoService.findExtById(custId));
	model.addAttribute("levelList", custInfoService.findAllLevel());
	model.addAttribute("statusList", CustStatusEnum.values());
	model.addAttribute("fromList", CustFromEnum.values());
	return "oem/user_detail";
  }

  @OEMAuthority(authorityRole = AuthorityRoleEnum.ADMIN)
  @RequestMapping(value = "/oem/levellist", method = RequestMethod.GET)
  public String userList(Model model) {
	model.addAttribute("levelList", custInfoService.findAllLevelWithCount());
	model.addAttribute("pageId", MenuEnum.OEM_ACCOUNT_LEVEL.getIndex());
	return "oem/level_list";
  }
}
